package org.jpro.modules.system.entity;

import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.jpro.common.bean.BaseEntity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * <P>功能描述 : 用户entity </P>
 * <P>Method Name : User </P>
 * <P>Author : 王泽浩 </P>
 * <P>E-Mail : 1028625100@qq.com </P>
 * <P>Date : 2017-03-03 23:41:36</P>
 * @since 1.0
 */
@Getter
@Setter
@Entity
@DynamicUpdate
@DynamicInsert
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "user")
@JsonIgnoreProperties("userRoles")
public class User extends BaseEntity<Integer> implements java.io.Serializable {

    // Fields
    private static final long serialVersionUID = 1L;

    @Column(name = "login_name", nullable = false, length = 20)
    private String loginName;

    @Column(name = "name", nullable = false, length = 20)
    private String name;

    @Column(name = "password", nullable = false)
    private String password;

    // 不持久化到数据库，也不显示在Restful接口的属性.
    @Transient
    @JsonIgnore
    private String plainPassword;

    @Column(name = "salt")
    private String salt;

    @Column(name = "birthday", length = 19)
    private Timestamp birthday;

    @Column(name = "gender")
    private Short gender;

    @Column(name = "email", length = 50)
    private String email;

    @Column(name = "phone", length = 20)
    private String phone;

    @Column(name = "icon", length = 500)
    private String icon;

    @Column(name = "create_date", length = 19)
    private Timestamp createDate;

    @Column(name = "state", length = 1)
    private String state;

    @Column(name = "description")
    private String description;

    @Column(name = "login_count")
    private Integer loginCount;

    @Column(name = "previous_visit", length = 19)
    private Timestamp previousVisit;

    @Column(name = "last_visit", length = 19)
    private Timestamp lastVisit;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
    private Set<UserRole> userRoles = new HashSet<UserRole>(0);

    // Constructors
    public User(Integer id) {
        super(id);
    }

    /**
     * minimal constructor
     */
    public User(String loginName, String name, String password) {
        this.loginName = loginName;
        this.name = name;
        this.password = password;
    }

}